﻿using Microsoft.AspNetCore.Mvc;
using 天离安全_天擎.Tl_Class_Services;
using 天离安全_天擎.Tl_SerVices;

namespace 天离安全_天擎.Controllers.Cache_Controller
{
    /// <summary>
    /// 缓存信息
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class GetCacheController : Controller
    {
        private readonly Tl_SerVices.GetMemoryCaching_Services _cacheService;
        /// <summary>
        /// 主函数定义
        /// </summary>
        /// <param name="cacheService"></param>
        public GetCacheController(GetMemoryCaching_Services cacheService)
        {
            _cacheService = cacheService;
        }
        /// <summary>
        /// 根据key来设置缓存信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost("set")]
        public async Task<IActionResult> Set([FromBody] GetCacheDefault item)
        {
            await _cacheService.SetCacheAsync(item.Key, item.Value);
            return Ok(new { status = "200", key = item.Key, value = item.Value, time = DateTime.Now.ToString()});
        }
        /// <summary>
        /// 根据KEY来获取缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet("get/{key}")]
        public async Task<IActionResult> Get(string key)
        {
            var cachedValue = await _cacheService.GetCacheAsync(key);
            if (cachedValue != null)
            {
                return Ok(new { status = "200",  value = cachedValue, time = DateTime.Now.ToString()});
            }
            return NotFound("获取到缓存信息");
        }
        /// <summary>
        /// 清除单个缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpDelete("clear/{key}")]
        public async Task<IActionResult> Clear(string key)
        {
            await _cacheService.ClearCacheAsync(key);
            return Ok(new { status = "200", value = "已清除：" + key + "的缓存信息", time = DateTime.Now.ToString() });
        }
        /// <summary>
        /// 清除所有缓存信息
        /// </summary>
        /// <returns></returns>
        [HttpDelete("clearAll")]
        public async Task<IActionResult> ClearAll()
        {
            await _cacheService.ClearAllCacheAsync();
            return Ok(new { status = "200", value = "已清除所有缓存信息", time = DateTime.Now.ToString() });
        }
    }
}
